package com.speedchina.doormanager.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.speedchina.doormanager.dao.DoorPlateDao;
import com.speedchina.doormanager.dao.TaskDao;
import com.speedchina.doormanager.domain.DoorPlate;
import com.speedchina.doormanager.domain.Task;
import com.speedchina.doormanager.dto.DoorPlateTaskDTO;
import com.speedchina.doormanager.dto.TaskDTO;
import com.speedchina.doormanager.enums.DoorPlateStatusEnum;
import com.speedchina.doormanager.enums.TaskTypeEnum;
import com.speedchina.doormanager.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskDao taskDao;

    @Autowired
    private DoorPlateDao doorPlateDao;


    @Override
    public List<TaskDTO> findAllTaskByTaskType(String type) {
        EntityWrapper taskEntityWrapper = new EntityWrapper<Task>();
        taskEntityWrapper.eq("taskType", type);
        taskEntityWrapper.orderBy("add_time", false);
        List<Task> taskList = taskDao.selectList(taskEntityWrapper);
        List<DoorPlate> doorPlateList = new ArrayList<>();
        List<TaskDTO> taskDTOList = new ArrayList<>();
        for (Task task : taskList) {
            doorPlateList = doorPlateDao.findAllByTaskId(task.getId());
            TaskDTO taskDTO = new TaskDTO();
            taskDTO.setTask(task);
            int finishNumber = 0;
            int totalNumber = 0;
            for (DoorPlate doorPlate : doorPlateList) {
                if (TaskTypeEnum.ANZHUANG.getStatus().equals(type)) {
                    if (DoorPlateStatusEnum.YIANZHUANG.getStatus().equals(doorPlate.getStatus())) {
                        finishNumber++;
                    }
                } else if (TaskTypeEnum.XUNJIAN.getStatus().equals(type)) {
                    if (DoorPlateStatusEnum.YIXUNJIAN.getStatus().equals(doorPlate.getStatus())) {
                        finishNumber++;
                    }
                } else if (TaskTypeEnum.WEIHU.getStatus().equals(type)) {
                    if (DoorPlateStatusEnum.YIWEIHU.getStatus().equals(doorPlate.getStatus())) {
                        finishNumber++;
                    }
                }
                totalNumber++;
            }
            taskDTO.setFinishNumber(finishNumber);
            taskDTO.setTotalNumber(totalNumber);
            taskDTOList.add(taskDTO);
        }
        return taskDTOList;
    }

    @Override
    public DoorPlateTaskDTO findTaskDTOById(String taskId) {
        List<DoorPlate> doorPlateList = doorPlateDao.findAllByTaskId(taskId);
        Task task = taskDao.selectById(taskId);
        DoorPlateTaskDTO doorPlateTaskDTO = new DoorPlateTaskDTO();
        doorPlateTaskDTO.setDoorPlateList(doorPlateList);
        doorPlateTaskDTO.setTask(task);
        int finishNumber = 0;
        int totalNumber = 0;
        for (DoorPlate doorPlate : doorPlateList) {

            if (TaskTypeEnum.ANZHUANG.getStatus().equals(task.getTaskType())) {
                if (DoorPlateStatusEnum.YIANZHUANG.getStatus().equals(doorPlate.getStatus())) {
                    finishNumber++;
                }
            } else if (TaskTypeEnum.XUNJIAN.getStatus().equals(task.getTaskType())) {
                if (DoorPlateStatusEnum.YIXUNJIAN.getStatus().equals(doorPlate.getStatus())) {
                    finishNumber++;
                }
            } else if (TaskTypeEnum.WEIHU.getStatus().equals(task.getTaskType())) {
                if (DoorPlateStatusEnum.YIWEIHU.getStatus().equals(doorPlate.getStatus())) {
                    finishNumber++;
                }
            }
            totalNumber++;
        }
        doorPlateTaskDTO.setFinishNumber(finishNumber);
        doorPlateTaskDTO.setTotalNumber(totalNumber);
        return doorPlateTaskDTO;
    }
}
